package com.zhiyou100.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhiyou100.model.User;
import com.zhiyou100.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserService userService;
    @RequestMapping("/list")
    public String listUser(@RequestParam Map map, @RequestParam(defaultValue = "1") int pageNum, HttpServletRequest request){
        System.out.println("UserController-map : "+map);
        PageHelper.startPage(pageNum, 5);
        List<User> users = userService.listUser(map);
        PageInfo<User> pageInfo = new PageInfo<User>(users);
        request.setAttribute("map",map);
        request.setAttribute("users",users);
        request.setAttribute("pageInfo",pageInfo);
        return "forward:/behind/user/listUser.jsp";
    }

    @RequestMapping("/addUser")
    public String addUser(@RequestParam(defaultValue = "0") String strId, User user){
        int id = Integer.parseInt(strId);
        if(id == 0){
            System.out.println("要添加的User是   :   "+user);
            Date date = new Date();
            SimpleDateFormat sim = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String createTime = sim.format(date);
            user.setCreateTime(createTime);
            int i = userService.addUser(user);
            System.out.println("添加User返回的受影响行数是   : "+i);
        }else {
            System.out.println("要更新的UserId是 : "+id);
            int  i  = userService.updateUser(user);
            System.out.println("更新User返回的受影响行数是   : "+i);
        }
        return "redirect:/user/list.do";

    }



    @RequestMapping("/lookUser")
    public String lookUser(int id,HttpServletRequest request){
        System.out.println("要查看修改的id是 : "+id);
        User user =  userService.lookUser(id);
        request.setAttribute("user",user);
        return "forward:/behind/user/lookUser.jsp";

    }
    @RequestMapping("/lookUser2")
    public String lookUser2(int id,HttpServletRequest request){
        System.out.println("要查看修改的id是 : "+id);
        User user =  userService.lookUser(id);
        request.setAttribute("user",user);
        return "forward:/behind/user/addUser.jsp";

    }
    @RequestMapping("/deleteAll")
    public String deleteAll(int[] ids){
        // System.out.println("要删除的数组是   :   "+ids.length);
        List<Integer> userIds = new ArrayList<>();
        for (int id:ids) {
            userIds.add(id);
        }
        // System.out.println("list集合的大小是  : "+videoIds.size());
        int i = userService.deleteAll(userIds);
        System.out.println("批量删除后返回的受影响行数是   :   "+i);
        return "redirect:/user/list.do";
    }
    @RequestMapping("/delete")
    private String delete(int id){
        System.out.println("要删除的id是    :    "+id);
        int i = userService.delete(id);
        System.out.println("单个删除后返回的受影响行数是   :   "+i);
        return "redirect:/user/list.do";

    }

}
